/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "MuRoSettingsFrame.h"

MuRoSettingsFrame::MuRoSettingsFrame( wxWindow* parent )
:
  SettingsFrame( parent ),
  iSpeedScale(100.00),
  iAngularSpeedScale(100.00)
{
  // We reset the text boxes to show the current speeds
  SetTextCtrlToCurrent();
}





void MuRoSettingsFrame::OnClose( wxCloseEvent& event )
{
  // We reset the text boxes to show the current speeds
  SetTextCtrlToCurrent();

  Show(false);
  event.Veto();
}

void MuRoSettingsFrame::OnSave( wxCommandEvent& event )
{
  // We check that the values are correct. If not, we reset the text boxes
  // to the original speeds
  //textCtrl_Speed
  //textCtrl_AngularSpeed

  double givenSpeedScale;
  double givenAngularSpeedScale;

  if(VerifyBoxToDouble(textCtrl_Speed, givenSpeedScale) == true)
    {
       if(VerifyBoxToDouble(textCtrl_AngularSpeed, givenAngularSpeedScale) == true)
         {
           // The speeds given are good, so we use them
           iSpeedScale = givenSpeedScale;
           iAngularSpeedScale = givenAngularSpeedScale;

         }

    }

  
  // We reset the text boxes to show the current speeds
  SetTextCtrlToCurrent();


  Show(false);
  //event.Veto();
}

void MuRoSettingsFrame::OnCancel( wxCommandEvent& event )
{
  // We reset the text boxes to show the current speeds
  SetTextCtrlToCurrent();

  Show(false);
  //event.Veto();
}

//*****************************************************************************
//*****************************************************************************

bool MuRoSettingsFrame::VerifyBoxToDouble(wxTextCtrl* aTextCtrl, double &aValue)
{
  double tmp;
  
  // We check if the wxTextCtrl contains a double
  if(aTextCtrl->GetValue().ToDouble(&tmp) == true)
    {
      // We check that the double is between 0 -> 1
      if(tmp > 0.0 && tmp <= 100.00)
        {
          // Value is acceptable
          aValue = tmp;
          return true;


        } else
        {
          // Value was out of bounds
          return false;
        }


    } else
    {
      // Value was not even a double
      return false;
    }


}




//*****************************************************************************
//*****************************************************************************

/** Sets the speedscale to the given value
 *
 */
void MuRoSettingsFrame::SetSpeedScale(float aSpeedScale)
{
  iSpeedScale = aSpeedScale;

  // Make the changes show in the wx frame
  SetTextCtrlToCurrent();

}

//*****************************************************************************
//*****************************************************************************

/** Sets the angularspeedscale to the given value
 *
 */
void MuRoSettingsFrame::SetAngularSpeedScale(float aAngularSpeedScale)
{
  iAngularSpeedScale = aAngularSpeedScale;
  
  // Make the changes show in the wx frame
  SetTextCtrlToCurrent();
}

//*****************************************************************************
//*****************************************************************************
float MuRoSettingsFrame::GetSpeedScale() const
{
  // We return the scale value as a float ranging betwen 0...1
  return iSpeedScale*0.01;
}

//*****************************************************************************
//*****************************************************************************
float MuRoSettingsFrame::GetAngularSpeedScale() const
{
  // We return the scale value as a float ranging betwen 0...1
  return iAngularSpeedScale*0.01;
}


//*****************************************************************************
//*****************************************************************************

void MuRoSettingsFrame::SetTextCtrlToCurrent()
{
  // We show the current scale values in the wx settings frame
  textCtrl_Speed->SetValue(wxString::Format(wxT("%.1f"), iSpeedScale));
  textCtrl_AngularSpeed->SetValue(wxString::Format(wxT("%.1f"), iAngularSpeedScale));


}
